Skip to content

android: detect native ABI and API level correctly#24868

Closed
lunagl wants to merge 2 commits into
ziglang:masterfrom
lunagl:android-api-detection
Closed

android: detect native ABI and API level correctly#24868
lunagl wants to merge 2 commits into
ziglang:masterfrom
lunagl:android-api-detection

Conversation

@lunagl

@lunagl lunagl commented Aug 15, 2025

Copy link
Copy Markdown
Contributor

Fixes ABI detection and adds API level detection (#24630).

I was planning on also including a fix for #24860, but I am not sure how to proceed due to the faccessat syscall not supporting flags (#16606).

@lunagl lunagl force-pushed the android-api-detection branch from cd92d62 to bf8be8d Compare August 15, 2025 20:35
@alexrp alexrp self-assigned this Aug 15, 2025
Comment thread lib/std/zig/system.zig Outdated
Comment thread lib/std/zig/system.zig Outdated
Comment thread lib/std/zig/system.zig Outdated
@alexrp

alexrp commented Aug 16, 2025

Copy link
Copy Markdown
Member

Would be good to know what kind of Android system(s) this has been tested on.

@lunagl lunagl force-pushed the android-api-detection branch from bf8be8d to a0c77fe Compare August 16, 2025 14:23
Comment thread lib/std/zig/system.zig Outdated
Comment thread lib/std/zig/system.zig Outdated
@lunagl

lunagl commented Aug 16, 2025

Copy link
Copy Markdown
Contributor Author

I have tested this on a Google Pixel 7 running Android 16 (API 36). Since it only relies on /system/bin/getprop and the ro.build.version.sdk property, it should be pretty universally supported.

@lunagl lunagl force-pushed the android-api-detection branch from a0c77fe to b31d075 Compare August 16, 2025 14:38
Comment thread lib/std/zig/system.zig
@lunagl lunagl force-pushed the android-api-detection branch from b31d075 to 823c738 Compare August 16, 2025 15:40
Comment thread lib/std/zig/system.zig Outdated
Comment thread lib/std/zig/system.zig Outdated
}

fn detectAndroidApiLevel() !u32 {
var alloc_buf: [384]u8 = undefined;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this buffer size in particular?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Technically from what I can tell only argv plus it's elements and envp (which is empty) need to be allocated, but because spawn uses an ArenaAllocator it was difficult to determine the exact memory required (and I don't know if it's 100% deterministic, I believe 256 worked once and then failed on another try), so I started with 128, tried 256, and then 384 (256 + 128) as a value that didn't OOM. It's not super pretty I agree, but since we don't have a proper heap allocator here I'm not sure what else to do

@rohlem rohlem Aug 17, 2025

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know a good strategy for this either, but if this is a heuristic/guessed value, then a comment that documents this fact for the future would be helpful.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should just bite the bullet and make resolveTargetQuery take an allocator? I haven't looked in depth, but a quick grep suggests most call sites have an allocator of some sort available.

@lunagl lunagl Aug 26, 2025

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think my understanding of the codebase is deep enough to make that call, I could certainly implement it tho. Right now it would only be used for the API level detection, I'm not sure if other parts of resolveTargetQuery would benefit from an allocator as well. What do you think?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you want to take a stab at it, please go ahead, though I suggest doing it as a separate commit within the PR.

Comment thread lib/std/zig/system.zig Outdated
@lunagl lunagl force-pushed the android-api-detection branch 5 times, most recently from ebc9a98 to a59f1ff Compare August 20, 2025 20:31
@lunagl lunagl force-pushed the android-api-detection branch 3 times, most recently from 4de90da to 467ac61 Compare August 27, 2025 17:03
@lunagl lunagl requested a review from alexrp August 28, 2025 13:20
Comment thread lib/std/zig/system.zig Outdated
@lunagl lunagl force-pushed the android-api-detection branch from 467ac61 to 63ef488 Compare August 29, 2025 11:58
@lunagl lunagl force-pushed the android-api-detection branch 2 times, most recently from c21cf94 to 7e00967 Compare September 5, 2025 15:21
ABI detection previously did not take into account the non-standard
directory structure of Android. This has been fixed.

The API level is detected by running `getprop ro.build.version.sdk`,
since we don't want to depend on bionic, and reading system properties
ourselves is not trivially possible.
For now the allocator is only used in `detectAndroidApiLevel`, but there
are probably other places in the detection logic that could benefit from
this too.
@lunagl lunagl force-pushed the android-api-detection branch from 7e00967 to 7a72618 Compare September 6, 2025 01:53
@lunagl

lunagl commented Sep 6, 2025

Copy link
Copy Markdown
Contributor Author

I'm trying to figure out why the pipelines are failing here, the errors seem unrelated to my changes. Any ideas?

@alexrp

alexrp commented Sep 6, 2025

Copy link
Copy Markdown
Member

It's happening because we previously didn't exercise libc-less process spawning logic as part of the standard library tests, it seems.

The issue is #23262.

@andrewrk

Copy link
Copy Markdown
Member

This pull request is not ready for review because:

  • It has conflicts that must be resolved via rebasing against latest origin/master.
  • It is not passing the CI tests.

Since we have moved development to Codeberg, please open your pull request there if you would like to continue these efforts.

@andrewrk andrewrk closed this Nov 27, 2025
@alexrp alexrp removed their assignment Nov 27, 2025
@andrewrk

andrewrk commented Jan 9, 2026

Copy link
Copy Markdown
Member

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants